﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CollegeShop.Infrastructure.Entities.Entity;
using CollegeShop.Infrastructure.Entities;
using CollegeShop.Configuration;

namespace CollegeShop.Biz
{
    public class BaseInfoBiz
    {
        /// <summary>
        /// 获取销售单元
        /// </summary>
        /// <returns></returns>
        public static List<Salesunit> GetSalesUnit(bool loadDormitory=false)
        {
            if (!loadDormitory)
            {
                return new Salesunit().FindAll();
            }
            using (Context ctx = new Context())
            {
                var list = (from c in ctx.Salesunit
                            join _d in ctx.Dormitory
                             on c.ID equals _d.SalesUnitID into dg
                            from d in dg.DefaultIfEmpty()
                            select new { c, d })
                           .ToList()
                           .GroupBy(c => c.c.ID)
                           .Select(c => new Salesunit()
                           {
                               ID = c.FirstOrDefault().c.ID,
                               LinkMan = c.FirstOrDefault().c.LinkMan,
                               LinkPhone = c.FirstOrDefault().c.LinkPhone,
                               SalesUnitName = c.FirstOrDefault().c.SalesUnitName,
                               SalesUnitNamePY = c.FirstOrDefault().c.SalesUnitNamePY,
                               Dormitories = c.Select(o => 
                               {
                                   if(o.d != null)
                                   {
                                       return new Dormitory()
                                       {
                                           DormitoryName = o.d.DormitoryName,
                                           ID = o.d.ID,
                                           SalesUnitID = o.d.SalesUnitID
                                       };
                                   }
                                   return null;
                               }).Where(i=>i!= null).ToList()
                           }).ToList();
                return list;
            }
        }

        /// <summary>
        /// 获取楼栋
        /// </summary>
        /// <returns></returns>
        public static List<Dormitory> GetDormitories(bool loadunitName=false)
        {
            if (loadunitName)
            {
                using (Context ctx = new Context())
                {
                    var list = (from c in ctx.Dormitory
                                join _u in ctx.Salesunit
                                 on c.SalesUnitID equals _u.ID into ug
                                from u in ug.DefaultIfEmpty()
                                select new { c, u.SalesUnitName })
                               .ToList()
                               .Select(c =>
                               {
                                   var d = new Dormitory().Set(c.c);
                                   d.SalesUnitName = c.SalesUnitName;
                                   return d;
                               })
                               .ToList();
                    return list;

                }
            }
            return new Dormitory().FindAll();
        }

        /// <summary>
        /// 获取未设置销售单元的楼栋
        /// </summary>
        /// <returns></returns>
        public static List<Dormitory> GetFreeDormitories()
        {
            return new Dormitory().FindAll(c => c.SalesUnitID == 0);
        }
    }
}
